//
//  GoodsView.m
//  LoveFreshBeen
//
//  Created by Cyan on 2016/10/16.
//  Copyright © 2016年 魏根. All rights reserved.
//

#import "GoodsView.h"
#import "GoodsCell.h"
#import "HMHomeFirstModel.h"
#import "HMHomeClickViewController.h"
#import <MJRefreshGifHeader.h>

@interface GoodsView ()<UITableViewDelegate, UITableViewDataSource>

@property(nonatomic,strong)NSArray* modelArray;

@end

@implementation GoodsView



-(void)awakeFromNib {
    [super awakeFromNib];
    [self setupUI];
    
}



- (void)setupUI {
    
    MJRefreshGifHeader *header = [MJRefreshGifHeader headerWithRefreshingTarget:self refreshingAction:@selector(refreshData:)];
    
    UIImage *image1 = [UIImage imageNamed:@"v2_pullRefresh1"];
    
    UIImage *image2 = [UIImage imageNamed:@"v2_pullRefresh2"];
    
    [header setImages:@[image1] forState:MJRefreshStateIdle];
    
    [header setImages:@[image2] forState:MJRefreshStatePulling];
    
    [header setImages:@[image1,image2] forState:MJRefreshStateRefreshing];
    
    [header setTitle:@"下拉刷新" forState:MJRefreshStateIdle];
    
    [header setTitle:@"松手开始刷新" forState:MJRefreshStatePulling];
    
    [header setTitle:@"正在刷新" forState:MJRefreshStateRefreshing];
    
    header.lastUpdatedTimeLabel.hidden = YES;
    
    self.tb_goods.mj_header = header;
    
    [self addSubview:self.tb_goods];
    
    [self.tb_goods mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.leading.bottom.trailing.equalTo(self);
    }];
}

- (void)setData:(NSArray *)data {
    
    _data = data;
    NSMutableArray* tempArray = [NSMutableArray array];
    for (NSDictionary* dict in data) {
        [tempArray addObject:[HMHomeFirstModel modelWithDict:dict]];
    }
    self.modelArray = tempArray;
    [self.tb_goods reloadData];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.data.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
     GoodsCell *cell = [tableView dequeueReusableCellWithIdentifier:@"goods" forIndexPath:indexPath];
    cell.model = self.modelArray[indexPath.row];
    
    return cell;
}

- (UITableView *)tb_goods {
    if (!_tb_goods) {
        _tb_goods = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 200, 300) style:UITableViewStylePlain];
        self.tb_goods.delegate = self;
        self.tb_goods.dataSource = self;
        self.tb_goods.rowHeight = 70;
        self.tb_goods.separatorColor = [UIColor clearColor];
        self.tb_goods.backgroundColor = [UIColor whiteColor];
        [self.tb_goods registerNib:[UINib nibWithNibName:@"GoodsCell" bundle:nil] forCellReuseIdentifier:@"goods"];
    }
    return _tb_goods;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIView* customView = [[UIView alloc] initWithFrame:CGRectMake(10.0, 0.0, 300.0, 44.0)];
    
    UILabel * headerLabel = [[UILabel alloc] initWithFrame:CGRectZero];
    headerLabel.backgroundColor = [UIColor lightTextColor];
    headerLabel.opaque = NO;
    headerLabel.textColor = [UIColor darkGrayColor];
    headerLabel.highlightedTextColor = [UIColor whiteColor];
    headerLabel.font = [UIFont boldSystemFontOfSize:12];
    headerLabel.frame = CGRectMake(10.0, 0.0, 300.0, 30.0);

    headerLabel.text = self.headerTitle;
    
    [customView addSubview:headerLabel];
    
    return customView;
}

- (CGFloat) tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 44.0;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    HMHomeClickViewController *vc = [[HMHomeClickViewController alloc] init];
    
    GoodsCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    
    vc.hidesBottomBarWhenPushed = YES;
    
    vc.model = cell.model;
    
    [[self viewController].navigationController pushViewController:vc animated:YES];
 
}

// 返回当前控件的父视图所在的控制器

- (UIViewController *)viewController
{
    for (UIView* next = [self superview]; next; next = next.superview) {
        UIResponder *nextResponder = [next nextResponder];
        if ([nextResponder isKindOfClass:[UIViewController class]]) {
            return (UIViewController *)nextResponder;
        }
    }
    return nil;
}

- (void)refreshData:(MJRefreshGifHeader *)header{
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        
        [self.tb_goods reloadData];
        
        [self.tb_goods.mj_header endRefreshing];
    });
}

- (void)dealloc {
    
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}


@end
